package de.ret_world.openrpgstudio.mediamanager;

import buoy.event.CommandEvent;
import buoy.event.WindowClosingEvent;
import buoy.widget.*;
import de.ret_world.openrpgstudio.editor.Media;
import de.ret_world.openrpgstudio.editor.Project;
import de.ret_world.openrpgstudio.editor.gui.AudioPreviewWidget;
import de.ret_world.openrpgstudio.editor.gui.ImagePreviewWidget;
import de.ret_world.openrpgstudio.editor.helpers.Internationalization;
import java.awt.Insets;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.apache.log4j.Logger;

/**
 * This is a dialog called to import any media file.
 *
 * @author Emu
 */
public class MediaImportDialog {

    private BDialog parent;
    private BDialog dialog;
    private Path path;
    private Path path2;
    private int type;
    private BTextField txtID;
    private BTextField txtPath;
    private boolean ok;

    public MediaImportDialog(BDialog parent) {
        this.parent = parent;
    }

    /**
     * Shows the Dialog.
     */
    public boolean showDialog(Path path) {
        this.path = path.normalize();

        Internationalization intn = Internationalization.getInstance();
        Project project = Project.getInstance();
        LayoutInfo li = new LayoutInfo(LayoutInfo.WEST, LayoutInfo.HORIZONTAL, new Insets(5, 5, 5, 5), null);

        if (path.getFileName().toString().endsWith(".png")) {
            type = Media.IMAGE;
        } else if (path.getFileName().toString().endsWith(".wav")) {
            type = Media.SOUND;
        } else if (path.getFileName().toString().endsWith(".ogg")) {
            type = Media.MUSIC;
        } else {
            throw new RuntimeException("ERROR! " + path + " has no supported file type!");
        }

        dialog = new BDialog(parent, intn.getString("MM.IMPORT"), true);
        dialog.addEventLink(WindowClosingEvent.class, this, "close");

        ColumnContainer cc = new ColumnContainer();

        cc.add(new BLabel(intn.getString("MM.ID")), li);
        txtID = new BTextField(20);
        String id = path.getFileName().toString();
        id = id.substring(0, id.length() - 4);
        id = id.toUpperCase();
        txtID.setText(id);
        cc.add(txtID, li);
        cc.add(new BLabel(intn.getString("MM.PATH")), li);

        RowContainer rc = new RowContainer();
        String p = null;
        switch (type) {
            case Media.IMAGE:
                p = "images";
                break;
            case Media.SOUND:
                p = "sounds";
                break;
            case Media.MUSIC:
                p = "music";
                break;
        }
        path2 = project.getFolder(p);
        path2 = project.getFolder("root").relativize(path2);
        rc.add(new BLabel(File.separator + path2.toString() + File.separator), li);
        txtPath = new BTextField(19);
        txtPath.setText(path.getFileName().toString());
        rc.add(txtPath, li);

        cc.add(rc, li);

        if (type == Media.IMAGE) {
            ExplicitContainer ec = new ExplicitContainer();
            ImagePreviewWidget ipw = new ImagePreviewWidget(path, false);
            ec.add(ipw, new Rectangle(305, 150));
            cc.add(ec, new LayoutInfo(LayoutInfo.CENTER, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        } else {
            boolean continouus = false;
            if (type == Media.MUSIC) {
                continouus = true;
            }
            AudioPreviewWidget apw = new AudioPreviewWidget(path, continouus);
            cc.add(apw, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.BOTH, new Insets(5, 5, 5, 5), null));
        }

        BorderContainer bc2 = new BorderContainer();
        BButton btn = new BButton(intn.getString("OK"));
        btn.addEventLink(CommandEvent.class, this, "ok");
        bc2.add(btn, BorderContainer.EAST, li);
        btn = new BButton(intn.getString("CANCEL"));
        btn.addEventLink(CommandEvent.class, this, "close");
        bc2.add(btn, BorderContainer.CENTER, li);
        cc.add(bc2, new LayoutInfo(LayoutInfo.EAST, LayoutInfo.NONE, new Insets(5, 5, 5, 5), null));

        dialog.setContent(cc);
        dialog.pack();
        dialog.setResizable(false);
        dialog.setVisible(true);

        return ok;
    }

    private void close() {
        ok = false;
        dialog.dispose();
    }

    private void ok() {
        ok = true;
        Project project = Project.getInstance();

        path2 = path2.resolve(txtPath.getText());
        path2 = path2.normalize();
        try {
            Files.createDirectories(project.getFolder("root").resolve(path2.getParent()));
            Files.copy(path, project.getFolder("root").resolve(path2));
            project.getMediaManager().addMedia(new Media(type, txtID.getText(), path2));
            project.save();
        } catch (IOException ex) {
            Logger.getLogger(MediaImportDialog.class).error(ex);
        }

        dialog.dispose();
    }
}
